﻿using DevExpress.XtraTab;
using DevExpress.XtraEditors;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.InternalItems;
using DevExpress.XtraBars;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraLayout;
using System.Data;
using UniMana.Utils.Configs;
using UniMana.Utils.Configs;
using UniMana.Utils.Controls;

namespace UniMana.Data
{
    public class SysUtils
    {


        public static string SMESSAGE = "Bạn không có quyền thực hiện chức năng này";
        public static void SetRuleControl(object frmDotControls, string _strRule)
        {
            DBMain m_data = new DBMain();

            string _strUserName = SysGlobalVar.g_sta_sysUserLogin.Properties.UserName;
            System.Type type = frmDotControls.GetType();
            #region Lap qua cac control container tìm control de phan quyen
            if (type.Name == "ControlCollection")
            {
                Control.ControlCollection CConllection = (Control.ControlCollection)frmDotControls;
                if (CConllection.Count > 0)
                {
                    for (int i = 0; i < CConllection.Count; i++)
                    {
                        SetRuleControl(CConllection[i], _strRule);
                    }
                }
                else
                {
                    return;
                }
            }

            if (type.Name == "XtraTabControl")
            {
                XtraTabControl xtraTabControl = (XtraTabControl)frmDotControls;

                for (int i = 0; i < xtraTabControl.TabPages.Count; i++)
                {
                    SetRuleControl(xtraTabControl.TabPages[i], _strRule);
                }
                return;
            }



            if (type.Name == "RibbonControl")
            {
                RibbonControl ribbon = (RibbonControl)frmDotControls;
                //dt.Rows.Add(new object[] { ribbon.Name, null, _strRule });
                for (int i = 0; i < ribbon.Pages.Count; i++)
                {
                    //System_User_Rule sur = UniMana.Data.System_RuleData.CheckRoleByUsername(m_data, _strUserName, ribbon.Pages[i].Name);
                    //if (sur != null)
                    //{
                    //    if (sur.AllowAccess == true)
                    //    {
                    //        ribbon.Pages[i].Visible = true;
                    //    }
                    //    else
                    //    {
                    //        ribbon.Pages[i].Visible = false;
                    //    }
                    //}
                    //else
                    //{
                    //    //if (_strRule == "FrmTrainingManager" && _strUserName != "admin")
                    //    //{
                    //    //    ribbon.Pages[i].Visible = false;
                    //    //}
                    //}

                    SetRuleControl(ribbon.Pages[i], _strRule);
                }

                for (int i = 0; i < ribbon.Items.Count; i++)
                {
                    SetRuleControl(ribbon.Items[i], _strRule);
                }


                return;

            }

            if (type.Name == "PanelControl")
            {
                PanelControl panel = (PanelControl)frmDotControls;
                //dt.Rows.Add(new object[] { panel.Name, panel.Text, parentName });
                for (int i = 0; i < panel.Controls.Count; i++)
                {
                    SetRuleControl(panel.Controls[i], _strRule);
                }
                return;
            }

            if (type.Name == "XtraScrollableControl")
            {
                XtraScrollableControl xtraScrollableControl = (XtraScrollableControl)frmDotControls;
                for (int i = 0; i < xtraScrollableControl.Controls.Count; i++)
                {
                    SetRuleControl(xtraScrollableControl.Controls[i], _strRule);
                }
                return;
            }

            if (type.Name == "SplitContainerControl")
            {
                SplitContainerControl splitContainerControl = (SplitContainerControl)frmDotControls;
                for (int i = 0; i < splitContainerControl.Panel1.Controls.Count; i++)
                {
                    SetRuleControl(splitContainerControl.Panel1.Controls[i], _strRule);
                }
                for (int i = 0; i < splitContainerControl.Panel2.Controls.Count; i++)
                {
                    SetRuleControl(splitContainerControl.Panel2.Controls[i], _strRule);
                }
                return;
            }
            if (type.Name == "GroupBox")
            {
                GroupBox groupBox = (GroupBox)frmDotControls;
                for (int i = 0; i < groupBox.Controls.Count; i++)
                {
                    SetRuleControl(groupBox.Controls[i], _strRule);
                }
                return;
            }
            //SplitContainerControl
            if (type.Name == "GroupControl")
            {
                GroupControl group = (GroupControl)frmDotControls;
                //dt.Rows.Add(new object[] { group.Name, group.Text, _strRule });
                if (group.Controls.Count > 0)
                {
                    for (int i = 0; i < group.Controls.Count; i++)
                    {
                        //System_User_Rule sur = UniMana.Data.System_RuleData.CheckRoleByUsername(m_data, _strUserName, group.Controls[i].Name);
                        //if (sur != null)
                        //{
                        //    if (sur.AllowAccess == true)
                        //    {
                        //        group.Controls[i].Visible = true;
                        //    }
                        //    else
                        //    {
                        //        group.Controls[i].Visible = false;
                        //    }
                        //}
                        //else
                        //{
                        //    //if (_strRule == "FrmTrainingManager" && _strUserName != "admin")
                        //    //{
                        //    //    group.Controls[i].Visible = false;
                        //    //}
                        //}

                        SetRuleControl(group.Controls[i], _strRule);
                    }
                    return;
                }
                else
                {
                    return;
                }
            }

            if (type.Name == "LayoutControl")
            {
                LayoutControl layOut = (LayoutControl)frmDotControls;
                //dt.Rows.Add(new object[] { layOut.Name, layOut.Text, parentName });
                for (int i = 0; i < layOut.Controls.Count; i++)
                {
                    SetRuleControl(layOut.Controls[i], _strRule);
                }

            }

            if (type.Name == "LayoutControlGroup")
            {
                LayoutControlGroup layOutGroup = (LayoutControlGroup)frmDotControls;
                //dt.Rows.Add(new object[] { layOutGroup.Name, layOutGroup.Text, parentName });
                if (layOutGroup.Items.Count > 0)
                {
                    for (int i = 0; i < layOutGroup.Items.Count; i++)
                    {
                        SetRuleControl(layOutGroup.Items[i], _strRule);
                    }
                    return;
                }
                else
                {
                    return;
                }
            }

            if (type.Name == "BarDockControl")
            {
                BarDockControl barDock = (BarDockControl)frmDotControls;
                //dt.Rows.Add(new object[] { barDock.Name, barDock.Text, parentName });
                for (int i = 0; i < barDock.Controls.Count; i++)
                {
                    SetRuleControl(barDock.Controls[i], _strRule);
                }
                return;
            }
            if (type.Name == "XtraScrollableControl")
            {
                XtraScrollableControl xtraScrol = (XtraScrollableControl)frmDotControls; ;
                for (int i = 0; i < xtraScrol.Controls.Count; i++)
                {
                    SetRuleControl(xtraScrol.Controls[i], _strRule);
                }
                return;
            }
            if (type.Name == "XtraTabPage")
            {
                XtraTabPage xtraTabPage = (XtraTabPage)frmDotControls;
                //dt.Rows.Add(new object[] { xtraTabPage.Name, xtraTabPage.Text, parentName });
                for (int i = 0; i < xtraTabPage.Controls.Count; i++)
                {
                    SetRuleControl(xtraTabPage.Controls[i], _strRule);
                }
                return;
            }
            #endregion

            #region Phan quyen
            /*---Huu--if (type.Name == "xucBar")
            {
                xucBar xuc = (xucBar)frmDotControls;
                string strRuleName = _strRule;
                if (_strRule.Substring(0, 3) == "Frm")
                {
                    strRuleName = _strRule.Substring(3);
                }

                SetRuleToXucBar(xuc, strRuleName);

                return;
            }----*/

            if (type.Name == "BarLinkContainerItem")
            {
                BarLinkContainerItem barLinkContainer = (BarLinkContainerItem)frmDotControls;

                System_User_Rule sur = UniMana.Data.System_RuleData.CheckRoleByUsername(m_data, _strUserName, barLinkContainer.Name);
                if (sur != null)
                {
                    if (sur.AllowAccess == true)
                    {
                        barLinkContainer.Visibility = BarItemVisibility.Always;
                    }
                    else
                    {
                        barLinkContainer.Visibility = BarItemVisibility.Never;
                    }
                }
                else
                {
                    //if (_strRule == "FrmTrainingManager" && _strUserName != "admin")
                    //{
                    //    barLinkContainer.Visibility = BarItemVisibility.Never;
                    //}
                }
            }

            if (type.Name == "BarButtonItem")
            {
                BarButtonItem barBtn = (BarButtonItem)frmDotControls;
                //R r = new R();//Khởi tạo danh sách bbi băm quyền
                if (R.m_htR[barBtn.Name] != null)
                {
                    string bbiName = R.m_htR[barBtn.Name].ToString();

                    System_User_Rule sur = UniMana.Data.System_RuleData.CheckRoleByUsername(m_data, _strUserName, bbiName);
                    if (sur != null)
                    {
                        if (sur.AllowAccess == true)
                        {
                            barBtn.Visibility = BarItemVisibility.Always;
                        }
                        else
                        {
                            barBtn.Visibility = BarItemVisibility.Never;
                            
                        }
                    }
                    else
                    {
                        if (_strRule == "FrmTrainingManager" && _strUserName != "admin")
                        {
                            barBtn.Visibility = BarItemVisibility.Never;
                        }
                    }
                    return;
                }
            }
            else
            { 
                return; 
            }
            if (type.Name == "LayoutControlItem")
            {
                LayoutControlItem layOutControl = (LayoutControlItem)frmDotControls;
                //dt.Rows.Add(new object[] { layOutControl.Name, layOutControl.Text, parentName });
                return;
            }

            if (type.Name == "RibbonPage")
            {
                RibbonPage ribbonPage = (RibbonPage)frmDotControls;
                System_User_Rule sur = UniMana.Data.System_RuleData.CheckRoleByUsername(m_data, _strUserName, ribbonPage.Name);
                if (sur != null)
                {
                    if (sur.AllowAccess == true)
                    {
                        ribbonPage.Visible = true;
                    }
                    if (sur.AllowAccess == false)
                    {
                        ribbonPage.Visible = false;
                    }

                    for (int i = 0; i < ribbonPage.Groups.Count; i++)
                    {
                        SetRuleControl(ribbonPage.Groups[i], ribbonPage.Name);
                    }
                }
                //else
                //{
                //    if (_strRule == "FrmTrainingManager")
                //    {
                //        ribbonPage.Visible = false;
                //    }
                //}
                return;
            }

            if (type.Name == "RibbonPageGroup")
            {
                RibbonPageGroup rbbPageGroup = (RibbonPageGroup)frmDotControls;
                System_User_Rule sur = UniMana.Data.System_RuleData.CheckRoleByUsername(m_data, _strUserName, rbbPageGroup.Name);
                if (sur != null)
                {
                    if (sur.AllowAccess == true)
                    {
                        rbbPageGroup.Visible = true;
                    }
                    //if (sur.AllowAccess == false)
                    //{
                    //    rbbPageGroup.Visible = false;
                    //}
                }
                else
                {
                    //if (_strRule == "FrmTrainingManager")
                    //{
                   // rbbPageGroup.Visible = false;
                    //}
                }
                return;
            }

            if (type.Name == "SimpleButton")
            {
                SimpleButton barButton = (SimpleButton)frmDotControls;
                string strRuleName = _strRule;
                if (_strRule.Substring(0, 3) == "Frm")
                {
                    strRuleName = "bbi"+_strRule.Substring(3);
                }

                System_User_Rule sur = UniMana.Data.System_RuleData.CheckRoleByUsername(m_data, _strUserName, strRuleName);
                if (sur != null)
                {
                    if (barButton.Name == "btnAdd")
                    {
                        barButton.Visible = bool.Parse(sur.AllowSave.ToString());
                    }

                    if (barButton.Name == "btnRemove")
                    {
                        barButton.Visible = bool.Parse(sur.AllowSave.ToString());
                    }

                    if (barButton.Name == "btnAuto")
                    {
                        barButton.Visible = bool.Parse(sur.AllowSave.ToString());
                    }

                    if (barButton.Name == "btnSave")
                    {
                        barButton.Visible = bool.Parse(sur.AllowSave.ToString());
                    }
                }
                return;
            }

            #endregion

            
        }
        public static void GetControl(object frmDotControl, string _strParentName, DataTable dt)
        {
            System.Type type = frmDotControl.GetType();

            if (type.Name == "ControlCollection")
            {
                Control.ControlCollection CConllection = (Control.ControlCollection)frmDotControl;
                if (CConllection.Count > 0)
                {
                    for (int i = 0; i < CConllection.Count; i++)
                    {
                        GetControl(CConllection[i], _strParentName, dt);
                    }
                }
                else
                {
                    return;
                }
            }

            

            if (type.Name == "BarButtonItemLink")
            {
                BarButtonItemLink bbiLink = (BarButtonItemLink)frmDotControl;
                dt.Rows.Add(new object[] { bbiLink.Item.Name, bbiLink.DisplayCaption, _strParentName ,2});
            }

            if (type.Name == "BarLinkContainerItemLink")
            {
                BarLinkContainerItemLink bciContainerItemLink = (BarLinkContainerItemLink)frmDotControl;
                dt.Rows.Add(new object[] { bciContainerItemLink.Item.Name, bciContainerItemLink.DisplayCaption, _strParentName, 2 });

                BarLinkContainerItem bliContainerItem = (BarLinkContainerItem)bciContainerItemLink.Item;
                for (int i = 0; i < bliContainerItem.ItemLinks.Count; i++)
                {
                    GetControl(bliContainerItem.ItemLinks[i], bciContainerItemLink.Item.Name, dt);
                }
            }

            if (type.Name == "RibbonControl")
            {
                RibbonControl ribbon = (RibbonControl)frmDotControl;
                //dt.Rows.Add(new object[] { ribbon.Name, null, _strRule });
                for (int i = 0; i < ribbon.Pages.Count; i++)
                {
                    GetControl(ribbon.Pages[i], ribbon.Name, dt);
                }

                return;
            }

            if (type.Name == "RibbonPage")
            {
                RibbonPage ribbonPage = (RibbonPage)frmDotControl;
                dt.Rows.Add(new object[] { ribbonPage.Name, ribbonPage.Text, null, 0 });
                for (int i = 0; i < ribbonPage.Groups.Count; i++)
                {
                    GetControl(ribbonPage.Groups[i], ribbonPage.Name, dt);
                }
                return;
            }

            if (type.Name == "RibbonPageGroup")
            {
                RibbonPageGroup rbbPageGroup = (RibbonPageGroup)frmDotControl;

                dt.Rows.Add(new object[] { rbbPageGroup.Name, rbbPageGroup.Text, _strParentName, 1 });

                for (int i = 0; i < rbbPageGroup.ItemLinks.Count; i++)
                {
                    GetControl(rbbPageGroup.ItemLinks[i], rbbPageGroup.Name, dt);
                    
                }
                return;
            }

        }
        
        public static void SetRuleToXucBar(xucBar xuc, string strRuleID)
        {
            DBMain m_data = new DBMain();
            string _strUserName = SysGlobalVar.g_sta_sysUserLogin.Properties.UserName;
            System_User_Rule sys_User_Rule = UniMana.Data.System_RuleData.CheckRoleByUsername(m_data, _strUserName, strRuleID);

            try
            {
                if(xuc.AddNewVisible!=false)
                xuc.AddNewVisible = sys_User_Rule.AllowAdd.Value;
            }
            catch { }

            try
            {
                if(xuc.EditVisible!=false)
                xuc.EditVisible = sys_User_Rule.AllowEdit.Value;
            }
            catch { }

            try
            {
                if (xuc.DeleteVisible != false)
                xuc.DeleteVisible = sys_User_Rule.AllowDelete.Value;
            }
            catch { }

            try
            {
                if (xuc.SaveVisible != false)
                xuc.SaveVisible = sys_User_Rule.AllowSave.Value;
            }
            catch { }

            try
            {
                if (xuc.PrintVisible != false)
                xuc.PrintVisible = sys_User_Rule.AllowPrint.Value;
            }
            catch { }

            try
            {
                if (xuc.ImportExcelVisible != false)
                xuc.ImportExcelVisible = sys_User_Rule.AllowImport.Value;
            }
            catch { }


            try
            {
                if (xuc.ExportExcelVisible != false)
                xuc.ExportExcelVisible = sys_User_Rule.AllowExport.Value;
            }
            catch { }

        }

        public static bool CheckSaveByRule( string strRuleID, bool showMesgBox = true)
        {
            DBMain m_data = new DBMain();       
            bool result = false;
            var r = System_User_RuleData.GetCurrentRuleOfCurrentUser(m_data, strRuleID);
            if (r != null)
            {
                result = r.AllowSave.Value;
            }
            if (result == false && showMesgBox==true)
            {
                MsgConfig.ShowErrorDialog(SMESSAGE);
            }
            return result;
        }
        public static bool CheckAddByRule(string strRuleID, bool showMesgBox = true)
        {
            DBMain m_data = new DBMain();
            bool result = false;
            var r = System_User_RuleData.GetCurrentRuleOfCurrentUser(m_data, strRuleID);
            if (r != null)
            {
                result = r.AllowAdd.Value;
            }
            if (result == false && showMesgBox)
            {
                MsgConfig.ShowErrorDialog(SMESSAGE);
            }
            return result;
        }
        public static bool CheckEditByRule(string strRuleID, bool showMesgBox = true)
        {
            DBMain m_data = new DBMain();
            bool result =false;
            var r = System_User_RuleData.GetCurrentRuleOfCurrentUser(m_data, strRuleID);
            if(r!=null)
            {
                result = r.AllowEdit.Value;
            }
            if (result == false && showMesgBox)
            {
                MsgConfig.ShowErrorDialog(SMESSAGE);
            }
            return result;
        }
        public static bool CheckDeleteByRule(string strRuleID, bool showMesgBox = true)
        {
            DBMain m_data = new DBMain();
            bool result = false;
            var r = System_User_RuleData.GetCurrentRuleOfCurrentUser(m_data, strRuleID);
            if (r != null)
            {
                result = r.AllowDelete.Value;
            }
            if (result == false && showMesgBox)
            {
                MsgConfig.ShowErrorDialog(SMESSAGE);
            }
            return result;
        }
        public static bool CheckAccessByRule(string strRuleID, bool showMesgBox = true)
        {
            DBMain m_data = new DBMain();
            bool result = false;
            var r = System_User_RuleData.GetCurrentRuleOfCurrentUser(m_data, strRuleID);
            if (r != null)
            {
                result = r.AllowAccess.Value;
            }
            if (result == false && showMesgBox)
            {
                MsgConfig.ShowErrorDialog(SMESSAGE);
            }
            return result;
        }
        public static bool CheckImportByRule(string strRuleID, bool showMesgBox = true)
        {
            DBMain m_data = new DBMain();
            bool result = false;
            var r = System_User_RuleData.GetCurrentRuleOfCurrentUser(m_data, strRuleID);
            if (r != null)
            {
                result = r.AllowImport.Value;
            }
            if (result == false && showMesgBox)
            {
                MsgConfig.ShowErrorDialog(SMESSAGE);
            }
            return result;
        }
        public static bool CheckExportByRule(string strRuleID)
        {
            DBMain m_data = new DBMain();
            bool result = false;
            var r = System_User_RuleData.GetCurrentRuleOfCurrentUser(m_data, strRuleID);
            if (r != null)
            {
                result = r.AllowExport.Value;
            }
            if (result == false)
            {
                MsgConfig.ShowErrorDialog(SMESSAGE);
            }
            return result;
        }
        public static bool CheckPrintByRule(string strRuleID)
        {
            DBMain m_data = new DBMain();
            bool result = false;
            var r = System_User_RuleData.GetCurrentRuleOfCurrentUser(m_data, strRuleID);
            if (r != null)
            {
                result = r.AllowExport.Value;
            }
            if (result == false)
            {
                MsgConfig.ShowErrorDialog(SMESSAGE);
            }
            return result;
        }
       
    }
}
